#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

//同样有头节点data储存个数
typedef struct Node {
    int data;
    struct Node *next;
} Node;

Node *initStack() 
{
    Node *S = (Node *)malloc(sizeof(Node));
    S->data = 0;
    S->next = NULL;
    return S;
}

int isEmpty(Node *S) 
{
    if (S->data == 0 || S->next == NULL) {
        return true;
    }
    return false;
}

int push(Node *S, int data)
{
    Node *node = (Node *)malloc(sizeof(Node));
    node->data = data;
    node->next = S->next; //这里考虑了两种情况
    S->next = node;
    S->data++;
}

int pop(Node *S) 
{
    if (isEmpty(S)) return -1;
    Node *del = S->next;
    int data = del->data;
    S->next = del->next;
    free(del);
    S->data--;
    return data;
}

int getTop(Node *S)
{
    if (isEmpty(S)) return -1;
    return S->next->data;
}

void printStack(Node *S)
{
    Node *tmp = S->next;    
    while (tmp) {
        printf("%d->", tmp->data);
        tmp = tmp->next;
    }    
    printf("NULL\n");
}

int main(void)
{
    Node *S = initStack();
    printf("getTop:%d\n", getTop(S));
    push(S, 1);
    push(S, 2);
    push(S, 3);
    push(S, 4);
    printf("getTop:%d\n", getTop(S));
    printStack(S);
    printf("%d\n", pop(S));
    printf("%d\n", pop(S));
    printStack(S);
    printf("个数：%d\n", S->data);

    return 0;
}